home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Communications / UUCon / Source / ConController.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  8.5 KB  |  393 lines

  1. /*
  2.  
  3.   Ronin Consulting, Inc.
  4.     Copyright (C) 1992, Nicholas Christopher (nwc@gun.com)
  5.  
  6.     This library is free software; you can redistribute it and/or
  7.     modify it under the terms of the GNU Library General Public
  8.     License as published by the Free Software Foundation; either
  9.     version 2 of the License, or (at your option) any later version.
  10.  
  11.     This library is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.     Library General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU Library General Public
  17.     License along with this library; if not, write to the Free
  18.     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. */
  21. #import <pwd.h>
  22. #import <libc.h>
  23. #import "version.h"
  24. #import "Defaults.h"
  25. #import "EnhancedText.h"
  26. #import "EnhancedApp.h"
  27. #import "Subprocess.h"
  28. #import "ConController.h"
  29. #import "LogController.h"
  30. #import "InfoController.h"
  31. #import "SweepView.h"
  32. #import <appkit/ScrollView.h>
  33. #import <appkit/PopUpList.h>
  34. #import <appkit/Window.h>
  35. #import <appkit/Matrix.h>
  36. #import <string.h>
  37.  
  38. static char tempFile[30] =  "/tmp/.UUCon.XXXXXX"; 
  39. static void sweepIncrement(DPSTimedEntry teNumber, double now, void *userData);
  40.  
  41. @interface ConController (PRIVATE)         /* Private methods */
  42. -_startSweep;
  43. -_endSweep;
  44. @end
  45.  
  46. @implementation ConController
  47.  
  48. - init
  49. {
  50.    [super init];
  51.  
  52.    [NXApp appDirectory];             /* cd to Apps directory so files in wrapper can be found */
  53.    defaults = [Defaults new];         
  54.    teNum = (DPSTimedEntry)0;
  55.    procType = NO_SPROC;
  56.    logfile = syslog = nil;
  57.    return self;
  58. }
  59.  
  60.  
  61. - pollHost:sender
  62. {
  63.  
  64.    const char *host = [hostsPopUp title];
  65.    char buff[80 + strlen(host)];
  66.  
  67.    if(procType != NO_SPROC)
  68.        return self;
  69.  
  70.    sprintf(buff,"Polling host: %s\n", host);
  71.    [consoleLog appendString: buff];
  72.  
  73.    [self _startSweep];
  74.  
  75.    if(*[defaults get: "DebugPoll"] == 'Y' && !auditProc)
  76.    {
  77.       auditFile = mktemp(tempFile);         /* build a temporary audit file */
  78.       sprintf(buff,"/usr/lib/uucp/uucico -r1 -s%s -x9 2> %s", host, auditFile);
  79.    }
  80.    else
  81.    {
  82.       [consoleLog appendString: "\nStarting to Poll - check log tool to watch progress\n"];
  83.       sprintf(buff,"uupoll %s", host);
  84.    }
  85.  
  86.    procType = POLL_HOST;
  87.    subProc = [[Subprocess alloc] init: buff withDelegate: self];
  88.  
  89.    if(*[defaults get: "DebugPoll"] == 'Y')
  90.    {
  91.       sprintf(buff, "tail -f %s", auditFile);
  92.       auditProc = [[Subprocess alloc] init: buff withDelegate: self];
  93.    }
  94.  
  95.    return self;
  96. }
  97.  
  98. - getBPS: sender
  99. {
  100.    char cmd[512] = "tail -14 /usr/spool/uucp/SYSLOG | fgrep bytes | \
  101.          awk '/failed/ { printf \" %8d bytes %8s (%8s) in %7.2f sec FAILED\\n\",\
  102.          $7, $5, $2, $9 } \
  103.          { if ( $7 > 100 && ($9 + 0) > 0) \
  104.             { bytes += $7; seconds += $9; \
  105.             printf \"%8d bytes %8s(%s) in %7.2f\\n\", $7, $5, $2, $9}} \
  106.          END { printf \"\\nAvg %8.2f bps\\n\", (bytes/seconds) * 8 }'"; 
  107.  
  108.    if (procType != NO_SPROC)
  109.        return self;
  110.  
  111.    [self _startSweep];
  112.    [consoleLog appendString: "Speed of last 7 jobs\n"];
  113.    procType = GET_BPS;
  114.    subProc = [[Subprocess alloc] init: cmd withDelegate: self];
  115.  
  116.    return self;
  117.                                           
  118. }
  119.  
  120. - hostsQueue:sender
  121. {
  122.    const char *host = [hostsPopUp title];
  123.    char buff[80 + strlen(host)];
  124.    
  125.    if(procType != NO_SPROC)
  126.        return self;
  127.  
  128.    [self _startSweep];
  129.  
  130.    sprintf(buff,"Queue for host: %s\n", host);
  131.    [consoleLog appendString: buff];
  132.    
  133.    sprintf(buff,"uuq -l -s%s -b%s", host, [defaults get: "BaudRate"]);
  134.  
  135.    procType = HOST_QUEUE;
  136.    subProc = [[Subprocess alloc] init: buff withDelegate: self];
  137.  
  138.    return self;
  139. }
  140.  
  141. - hostsSnap:sender
  142. {
  143.    if(procType != NO_SPROC)
  144.        return self;
  145.  
  146.    [self _startSweep];
  147.  
  148.    [consoleLog appendString: "Snapshot of uucp hosts\n"];
  149.    
  150.    procType = SNAP_HOSTS;
  151.    subProc = [[Subprocess alloc] init: "uusnap" withDelegate: self];
  152.  
  153.    return self;
  154. }
  155.  
  156. - setConsoleLog:sender
  157. {
  158.    consoleLog = [sender docView];
  159.    return self;
  160. }
  161.  
  162. - deleteJob:sender
  163. {
  164.    const char *host = [hostsPopUp title];
  165.    const char *job = [consoleLog selectedText];
  166.    char buff[80 + strlen(host) + strlen(job)];
  167.  
  168.    if(procType != NO_SPROC)
  169.        return self;
  170.  
  171.    [self _startSweep];
  172.  
  173.    sprintf(buff,"Deleting job %s for host: %s\n", job, host);
  174.    [consoleLog appendString: buff];
  175.    sprintf(buff, "uuq -s%s -d%s", host, job);
  176.    
  177.    procType = DELETE_JOB;
  178.    subProc = [[Subprocess alloc] init: buff withDelegate: self];
  179.  
  180.    return self;
  181. }
  182.  
  183. - keyWindowLogClear: sender
  184. {
  185.    id keyController = [[NXApp keyWindow] delegate];
  186.  
  187.  
  188.    if([keyController respondsTo: @selector(logClear)])
  189.        [keyController logClear];
  190.  
  191.    return self;
  192. }
  193.  
  194. - logClear
  195. {
  196.    [consoleLog empty: self];
  197.    return self;
  198. }
  199.  
  200. - syslog: sender
  201. {
  202.    if(!syslog)
  203.        syslog = [[LogController alloc] initForCommand: "tail -10f /usr/spool/uucp/SYSLOG" entitled: "SYSLOG" ];
  204.  
  205.    [syslog makeKeyAndOrderFront: self];
  206.  
  207.    return self;
  208. }
  209.  
  210.  
  211. - logfile: sender
  212. {
  213.  
  214.    if(!logfile)
  215.        logfile = [[LogController alloc] initForCommand: "tail -10f /usr/spool/uucp/LOGFILE" entitled: "LOGFILE" ];
  216.  
  217.    [logfile makeKeyAndOrderFront: self];
  218.  
  219.    return self;
  220. }
  221.  
  222. @end                         /* ConController */
  223.  
  224. @implementation ConController (PRIVATE)
  225.  
  226. -_startSweep
  227. {
  228.    [progress setIntValue: 0];
  229.    teNum = DPSAddTimedEntry(.02, sweepIncrement, &progress, NX_RUNMODALTHRESHOLD + 1);
  230.    return self;
  231. }
  232.  
  233. -_endSweep
  234. {
  235.    if(teNum)
  236.    {
  237.       DPSRemoveTimedEntry(teNum);
  238.       teNum = (DPSTimedEntry)0;
  239.    }
  240.  
  241.    [progress setIntValue: -1];
  242. //   [progress display];                 
  243.    return self;
  244. }
  245.  
  246. @end                         /* ConController (PRIVATE) */
  247.  
  248. @implementation ConController (ApplicationDelegate)
  249.  
  250. - appDidInit:sender
  251. {
  252.    /* if this program is set gid to daemon and L.sys is group readable to same we can debug */
  253.    setgid(getegid());    
  254.  
  255.    [infoController setVersionValue: version];
  256.    procType = BUILDING_MENU;
  257.    subProc = [[Subprocess alloc] init: "uuname" withDelegate: self];
  258.    [progress setIntValue: -1];
  259.    [progress setStepSize: 10];
  260.    return self;
  261. }
  262.  
  263. - appWillTerminate:sender
  264. {
  265.    if(procType != NO_SPROC)
  266.        [subProc free];
  267.  
  268.    if(auditProc)
  269.        [auditProc free];
  270.  
  271.    if(logfile)
  272.        [logfile free];
  273.  
  274.    if(syslog)
  275.        [syslog free];
  276.  
  277.    return self;
  278. }
  279.  
  280. @end                         /* ApplicationDelegate */
  281.  
  282.  
  283. @implementation ConController (SubprocessDelegate)
  284.  
  285. - subprocess:sender done:(int)exitStatus
  286. {
  287.    switch(procType)
  288.    {
  289.     case BUILDING_MENU:
  290.       [[hostsPopUp target] removeItem: [hostsPopUp title]];
  291.       [hostsPopUp setTitle: [[[[hostsPopUp target] itemList] cellAt:0:0] title]];
  292.                          /* choose the default host if one set */
  293.       if([[hostsPopUp target] indexOfItem: [defaults get: "DefaultHost"]] != -1)
  294.       [hostsPopUp setTitle: [defaults get: "DefaultHost"]];
  295.  
  296.       [consoleLog appendString: "Ready to go.\n"];
  297.  
  298.       if(*[defaults get: "ShowLog"] == 'Y')
  299.       [self logfile: self];
  300.  
  301.       if(*[defaults get: "ShowSysLog"] == 'Y')
  302.       [self syslog: self];
  303.  
  304.       [window makeKeyAndOrderFront: self];
  305.  
  306.       break;
  307.  
  308.     case HOST_QUEUE:
  309.       [consoleLog appendString: "\nQueue Complete\n"];
  310.       break;
  311.  
  312.     case SNAP_HOSTS:
  313.       [consoleLog appendString: "\nSnapshot Complete\n"];
  314.       break;
  315.  
  316.     case POLL_HOST:
  317.  
  318.       if(auditProc)
  319.       {
  320.      [consoleLog appendString:"\nDebug Poll done.\n"];
  321.      [NXApp delayedFree: auditProc];
  322.      auditProc = nil;
  323.      unlink(auditFile);
  324.       }
  325.       
  326.       break;
  327.  
  328.     case DELETE_JOB:
  329.       [consoleLog appendString: "\nDelete Complete\n"];
  330.       break;
  331.  
  332.     case GET_BPS:
  333.       [consoleLog appendString: "\nSpeed Listing Complete.\n"];
  334.       break;
  335.  
  336.     case NO_SPROC:
  337.       return self;                 /* don't want to free unknown */
  338.       break;
  339.    }
  340.  
  341.    if(sender == subProc)
  342.    {
  343.       procType = NO_SPROC;
  344.       [self _endSweep];
  345.    }
  346.    
  347.    [NXApp delayedFree: sender];
  348.    return self;
  349. }
  350.  
  351. - subprocess:sender output:(char *)buffer
  352. {
  353.  
  354.    [progress increment: self];
  355.  
  356.    switch(procType)
  357.    {
  358.     case BUILDING_MENU:
  359.       [[hostsPopUp target] addItem: buffer];
  360.       break;
  361.  
  362.     default:
  363.       [consoleLog appendString: buffer];
  364.       [consoleLog appendString: "\n"];
  365.       break;
  366.    }
  367.  
  368.    return self;
  369. }
  370.  
  371. - subprocess:sender stderrOutput:(char *)buffer
  372. {
  373.    [progress increment: self];
  374.  
  375.    [consoleLog appendString: buffer];
  376.    return self;
  377. }
  378.  
  379. - subprocess:sender error:(const char *)errorString
  380. {
  381.    [consoleLog appendString: "Failed to create subprocess.\n"];
  382.    return self;
  383. }
  384.  
  385.  
  386. @end
  387.  
  388.  
  389. void sweepIncrement(DPSTimedEntry teNumber, double now, void *userData)
  390. {
  391.    [*(id *)userData increment: nil];
  392. }
  393.